<?php

namespace App\Models;

use App\Models\traits\serializeDate;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Consultation extends Model
{
    use HasFactory, serializeDate;

    protected $guarded = [];

    public $appends = ['village_name'];
    //触发父级的updated_at时间，表示处理的时间
    protected $touches = ['relies'];

    public function getVillageNameAttribute()
    {
        return $this->village()->value("name");
    }

    public function village()
    {
        return $this->belongsTo(Village::class);
    }

    protected $casts = [
        'images' => 'array'
    ];

    //关联回复（回复是多态）
    public function relies()
    {
        return $this->morphMany(Reply::class, 'replyable');
    }

    //被user或者wxuser处理
    public function consultationable()
    {
        return $this->morphTo();
    }
}
